/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.hibernate;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.piaoyi.bean.Admin;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.AdminDAO;

/**
 * 功能描述:管理员操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class AdminHibernateDAO extends HibernateDaoSupport implements AdminDAO {

	private static final String LOAD_BY_ID = "from Admin where id = ?";
	private static final String LOAD_BY_USERNAME = "from Admin where username = ?";
	private static final String  REMOVE_BY_ID = "delete from Admin where id = ?";
	
	/**
	 * 保存Admin对象
	 * 
	 * @param admin
	 * 			Admin对象
	 * @return Admin
	 * 			Admin对象
	 */
	public Admin saveAdmin(Admin admin){
		this.getHibernateTemplate().save(admin);
		return admin;
	}
	/**
	 * 保存或更新Admin对象
	 * 
	 * @param admin
	 * 			Admin对象
	 * @return Admin
	 * 			Admin对象
	 */
	public Admin saveOrUpdateAdmin(Admin admin){
		this.getHibernateTemplate().saveOrUpdate(admin);
		return admin;
	}
	/**
	 * 保存Admin对象
	 * 
	 * @param admin
	 * 			Admin对象
	 * @return Admin
	 * 			Admin对象
	 */
	public Admin updateAdmin(Admin admin){
		this.getHibernateTemplate().update(admin);
		return admin;
	}
	
	/**
	 * 根据主键ID获取Admin对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Admin
	 * 			Admin对象
	 */
	public Admin findAdminById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Admin) l.get(0);
		}
	}
	
	/**
	 * 根据username获取Admin对象
	 * 
	 * @param username
	 * 			String
	 * @return Admin
	 * 			Admin对象
	 */
	public Admin findAdminByUsername(String username){
		List l = this.getHibernateTemplate().find(LOAD_BY_USERNAME,username);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Admin) l.get(0);
		}
	}
	
	/**
	 * 获取Admin个数
	 * 
	 * @return int
	 * 			Admin个数
	 */
	public long getAdminNum(){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Admin.class);
				c.setProjection(Projections.count("id"));
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Integer) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取Admin列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Admin对象列表
	 */
	public List findAdmins(final String orderby, final int ascOrDesc, final int firstResult, final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Admin.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
	
	/**
	 * 获取Admin列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Admin对象列表
	 */
	public List findAdmins(final String orderby, final int ascOrDesc){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Admin.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				return c.list();
			}
		});
	}
	
	/**
	 * 删除Admin对象
	 */
	public void removeAdminById(final long id){
	    getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
}
